//
//  YSGMyopiaControlController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2021/1/5.
//  Copyright © 2021 QDSG. All rights reserved.
//

import UIKit

class YSGMyopiaControlController: BaseViewController {
    
    private var headerView: YSGMyopiaHeaderView!
    
    @IBOutlet private weak var tableView: UITableView! {
        didSet {
            tableView.backgroundColor = .background
            tableView.tableFooterView = UIView()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "眼科检查"
        setupTableHeader()
    }


    private func setupTableHeader() {
        headerView = YSGMyopiaHeaderView(with: self,
                                         imageArr: ["icon_myopia_banner"],
                                         closure: { [weak self] (type) in
            switch type {
            case .reserve:
                self?.pushReserveVc(deviceId: nil, type: .hand)
            case .scan:
                self?.pushScanVc()
            case .record:
                self?.pushRecordListVc()
            }
        })
        headerView.frame = CGRect(x: 0, y: 0, width: screenW, height: 210)
        tableView.tableHeaderView = headerView
    }
    
    /// 跳转预约检查
    private func pushReserveVc(deviceId: String?, type: MyopiaReserveInputType) {
        let reserveVc = YSGMyopiaReserveController(inputType: type)
        if let id = deviceId { reserveVc.deviceId = id }
        navigationController?.pushViewController(reserveVc, animated: true)
    }
    
    private func pushScanVc() {
        let scanVC = QRCodeScanVC()
        scanVC.handlerResult = { [weak self] (jsonStr, scanFrom) in
            guard let self = self else { return }
            var deviceId: String?
            if let urlStr = jsonStr {
                let deviceIdStr = urlStr.components(separatedBy: "?").last
                deviceId = deviceIdStr?.components(separatedBy: "=").last
            }
            self.pushReserveVc(deviceId: deviceId, type: .scan)
        }
        self.navigationController?.pushViewController(scanVC, animated: true)
    }
    
    private func pushRecordListVc() {
        let recordList = YSGMyopiaRecordListController()
        navigationController?.pushViewController(recordList, animated: true)
    }
}
